package com.citybox.infrastructure.activity.persistence.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.citybox.domain.activity.ActivityInfoDo;
import com.citybox.infrastructure.activity.dataobject.ActivityInfo;
import com.citybox.infrastructure.activity.persistence.mapper.ActivityInfoMapper;
import com.citybox.infrastructure.activity.persistence.service.ActivityInfoService;
import com.citybox.infrastructure.file.dataobject.FileInfo;
import com.citybox.infrastructure.file.persistence.mapper.FileInfoMapper;
import com.citybox.shared.web.PageUtils;
import com.citybox.shared.web.page.TableDataInfo;
import com.github.pagehelper.PageInfo;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhao.x
 */
@Service
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo> implements ActivityInfoService {

    @Resource
    private FileInfoMapper fileInfoMapper;

    @Override
    public TableDataInfo<?> pageList(ActivityInfoDo activityInfoDo) {
        if(StringUtils.isEmpty(activityInfoDo.getAddressCode())){
            return TableDataInfo.error("The address code is a required field.");
        }
        // 1 分页
        PageUtils.startPage();
        // 2 查询list
        List<ActivityInfoDo> activityList = getBaseMapper().pageList(activityInfoDo);
        if (CollectionUtils.isEmpty(activityList)) {
            return TableDataInfo.success("The data is empty.");
        }
        // 3 批量获取所有图片ID
        Set<String> allPictureIds = activityList.stream()
                .filter(activity -> StringUtils.isNotBlank(activity.getActivityPictureId()))
                .flatMap(activity -> Arrays.stream(activity.getActivityPictureId().split(",")))
                .map(String::trim)
                .filter(id -> !id.isEmpty())
                .collect(Collectors.toSet());

        // 4 批量查询图片URL
        Map<String, String> pictureUrlMap;
        if (!allPictureIds.isEmpty()) {
            List<FileInfo> fileInfos = fileInfoMapper.selectList(
                    new LambdaQueryWrapper<FileInfo>()
                            .in(FileInfo::getId, allPictureIds)
            );
            pictureUrlMap = fileInfos.stream()
                    .collect(Collectors.toMap(
                            FileInfo::getId,
                            FileInfo::getFileUrl,
                            // 如果有重复key保留现有的
                            (existing, replacement) -> existing
                    ));
        } else {
            pictureUrlMap = new HashMap<>();
        }

        // 5 设置图片URL到活动对象
        if(!pictureUrlMap.isEmpty()){
            activityList.forEach(activity -> {
                if (StringUtils.isNotBlank(activity.getActivityPictureId())) {
                    List<String> pictureUrls = Arrays.stream(activity.getActivityPictureId().split(","))
                            .map(String::trim)
                            .filter(id -> !id.isEmpty())
                            .map(pictureUrlMap::get)
                            .filter(Objects::nonNull)
                            .collect(Collectors.toList());
                    activity.setActivityPictureUrls(pictureUrls);
                }
            });
        }

        return TableDataInfo.success(
                activityList,
                new PageInfo<>(activityList).getTotal()
        );
    }

    @Override
    public Boolean save(ActivityInfoDo activityInfoDo) {
        activityInfoDo.setCreateTime(new Date());
        return this.save(activityInfoDo.toEntity(ActivityInfo.class));
    }

}
